//! Testdriver for the musicplayer
/*!
	\file main.cpp
	\author Thomas Pfeiffer
	\date 22.11.2012
*/
#include <iostream>
#include <exception>
#include "IVIsitor.h"
#include "MediaLibrary.h"
#include "MusicPlayer.h"
#include "Album.h"
#include "Song.h"
#include "MusicCollection.h"

using namespace std;

//! A function which calculates random durations of songs
/*! 
	The function calculates random duration minutes between 0 and
	6 minutes and random duration secounds between 1 and 59 seconds.
	\returns The duration of a Song
*/
TDuration getDuration(){
	int min = rand() % 7;
	int sec = rand() % 59 + 1;
	TDuration dur;
	dur.Minutes = min;
	dur.Seconds = sec;
	return dur;
}
//! main
/*! 
	The testdriver for the MusicPlayer-Project
*/
int main() {
	try{
		//library and player
		MediaLibrary library;
		MusicPlayer player;
		//three albums
		Album* album1 = library.CreateAlbum("All Along the Watchtower", "U2");
		Album* album2 = library.CreateAlbum("Best of 2010", "AC/DC");
		Album* album3 = library.CreateAlbum("Stadium Arcadium","RHCP");
		//two collection
		MusicCollection* collection1 = library.CreateCollection("Party Hits");
		//songs which belong to albums
		Song* song1 = library.CreateSong("Pride", "U2", album1, getDuration());
		Song* song2 = library.CreateSong("Angel of Harlem", "U2", album1, getDuration());
		Song* song3 = library.CreateSong("Highway to Hell", "AC/DC", album2, getDuration());
		Song* song4 = library.CreateSong("Thunderstruck", "AC/DC", album2, getDuration());
		Song* song5 = library.CreateSong("Runaway Train", "AC/DC", album2, getDuration());
		Song* song6 = library.CreateSong("Charlie", "RHCP", album3, getDuration());
		Song* song7 = library.CreateSong("She looks to me", "RHCP", album3, getDuration());
		Song* song8 = library.CreateSong("Hard to concentrate", "RHCP", album3, getDuration());
		//songs that do not belong to albums
		Song* song9 = library.CreateSong("Pride", "U2", 0, getDuration());
		Song* song10 = library.CreateSong("Thunderstruck", "AC/DC", 0, getDuration());
		Song* song11 = library.CreateSong("Snow (Hey Oh)", "RHCP", 0, getDuration());
		//add all media
		player.AddMedia(album1);
		player.AddMedia(album2);
		player.AddMedia(album3);
		player.AddMedia(collection1);
		player.AddMedia(song1);
		player.AddMedia(song2);
		player.AddMedia(song3);
		player.AddMedia(song4);
		player.AddMedia(song5);
		player.AddMedia(song6);
		player.AddMedia(song7);
		player.AddMedia(song8);
		player.AddMedia(song9);
		player.AddMedia(song10);
		player.AddMedia(song11);
		//add songs to an album
		album1->Add(song1);
		album1->Add(song2);
		album2->Add(song3);
		album2->Add(song4);
		album2->Add(song5);
		album3->Add(song6);
		album3->Add(song7);
		album3->Add(song8);
		//add album and songs to music collection
		collection1->Add(album1);
		collection1->Add(album3);
		collection1->Add(song9);
		collection1->Add(song10);
		collection1->Add(song11);
		//play music
		player.Play();
		//search for music
		player.Search("a");
		player.Search("l");
		//seacrh for media which is not in the player
		player.Search("abcdef");
		//remove some songs
		player.RemoveMedia(song1);
		player.RemoveMedia(song2);
		player.RemoveMedia(song3);
		player.RemoveMedia(song4);
		//play again
		player.Play();
		//test GetDuration()
		TDuration dur;
		//duration of all songs in the player
		dur = player.GetDuration();
		cout << "Duration of all songs together: " << dur.Minutes << ":" << dur.Seconds << endl;
		dur = player.GetDuration("Charlie");
		cout << "Duration of Song Charlie: " << dur.Minutes << ":" << dur.Seconds << endl;
		//get duration of song that is not in the player
		dur = player.GetDuration("abcdef");
		cout << "Duration of abcdef: " << dur.Minutes << ":" << dur.Seconds << endl;
		//throw an exception
		album1->Add(0);
	}
	catch(bad_alloc & ex){
		cout << ex.what() << endl;
	}
	catch(exception & ex){
		cout << ex.what() << endl;
	}

	return 0;
}